/*
 * @Author: liangliang
 * @Date: 2023-07-25 14:08:45
 * @LastEditors: yusha
 * @LastEditTime: 2023-08-23 22:09:56
 * @Description:
 */
const path = require('path');
const CracoCSSModules = require('craco-css-modules');
const pkg = require('./package.json');

const getPublicPath = () => {
    const isTestBucket = () => {
        return ['dev', 'test01', 'test02', 'test03', 'test04'].includes(process.env.SNIFF_BUILD_ENV);
    };
    const isProdBucket = () => {
        return ['pre', 'prod'].includes(process.env.SNIFF_BUILD_ENV);
    };
    // 线上和预发环境规则
    const getProdPublicUrl = (env) => {
        return `https://page-service.theckb.com/${env}-${pkg.name}/present/`;
    };
    // 测试环境规则
    const getTestPublicUrl = (env) => {
        return `https://page-service-test.theckb.com/${env}-${pkg.name}/present/`;
    };
    const SNIFF_BUILD_ENV = process.env.SNIFF_BUILD_ENV;
    if(SNIFF_BUILD_ENV){
        if(isTestBucket()){
            return getTestPublicUrl(SNIFF_BUILD_ENV);
        }
        if(isProdBucket()){
            return getProdPublicUrl(SNIFF_BUILD_ENV);
        }
    }
};

const config = {
    webpack: {
        alias: {
            '@': path.resolve(__dirname, 'src/'),
        },
        configure: (webpackConfig, { env, paths }) => {
            if (webpackConfig && webpackConfig.output && env !== 'development') {
                // 主要是为了 cdn 域名转发的时候路径处理，觉得不应该侵入到打包过程中，因为开发者不应该知道转发路径到底是什么
                // 而且后期所有的 文件 emit 都应该丢到 oem 中，配置 plugin 的时候需要注意!!!
                // client-pre/present/logo512.png
                const publicPath = getPublicPath();
                if(publicPath){
                    webpackConfig.output.publicPath = publicPath;
                    // 'https://page-client-test.theckb.com/test02-oemclient/present';
                    // publicPath;
                }
            }

            return webpackConfig;
        }
    },
    plugins: [
        { plugin: CracoCSSModules }
    ],
};

module.exports = config;
